<template>
    <div class="sp-detail">
        <div class="sp-top" v-loading="loading">
            <el-form label-width="90px" size="mini" style="width: 50%" class="demo-form-inline">
                <el-form-item label="模板名称">
                    <el-input name="模板名称" v-model="specialTemplate.branchTemplate.templateName"></el-input>
                </el-form-item>
                <el-form-item label-width="0">
                    <el-col :span="12">
                        <el-form-item label="模板编号">
                            <el-input placeholder="自动生成" disabled v-model="specialTemplate.branchTemplate.templateCode"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="范围类型">
                            <el-select
                                name="boundType"
                                v-model="specialTemplate.branchTemplate.boundType"
                                clearable
                                @change="changeBoundType"
                                style="width: 100%"
                            >
                                <el-option
                                    v-for="itme in boundTypes"
                                    :key="itme.dictKey"
                                    :value="itme.dictKey"
                                    :stockBound="stockBound"
                                    :label="itme.dictKey + ' ' + itme.dictKeyName"
                                    :name="'boundType-' + itme.dictKey"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-form-item>
            </el-form>
        </div>
        <div class="sp-itme">
            <!-- <ItemBoundSetting
                style="height:400px"
                :isDisabled="true"
                @onHandleOperateOver="getBranchData"
                :investGroupType="specialTemplate.branchTemplate.boundType"
                :riskEntryBranchList="riskEntryBranchList"
            ></ItemBoundSetting> -->
            <BoundCfg0
                :stockBound="riskEntryBranchList"
                :showCategory="false"
                :investGroupType="specialTemplate.branchTemplate.boundType"
                operatePanelHeight="40vh"
                :operatePanelWidth="'50%'"
                :startParentId="2"
                @onHandleOperateOver="getBranchData"
                :key="specialTemplate.branchTemplate.boundType"
                :disabledSpliceMethods="true"
            ></BoundCfg0>
            <!-- operateType="edit" -->
        </div>
        <div class="sp-bout">
            <div class="sp-but-div">
                <el-button @click="openBranch">取消</el-button>
                <el-button name="确定" type="primary" :disabled="loading" @click="submitSpecial" v-if="this.itemData.type !== 'look-bound'"
                    >确定</el-button
                >
            </div>
        </div>
    </div>
</template>
<script lang="ts">
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
import DictionConstUtil, { SGU_CACHE } from '@/utils/diction-const-util';
import RiskIconButton from '@/common/components/risk-icon-button.vue';
import { mixins } from 'vue-class-component';
import RiskEmitter from '@/common/mixis/risk-emitter.vue';
import ItemBoundSetting from './item-bound-setting.vue';
import BoundCfg0 from '@/pages/baseparam/fund-group-setting/BoundCfg0.vue';

@Component({
    name: 'ItemBoundDetail',
    components: {
        RiskIconButton,
        ItemBoundSetting,
        BoundCfg0,
    },
})
export default class ItemBoundDetail extends mixins(RiskEmitter) {
    @Prop() itemData: any;
    riskEntryBranchList: any = []; // 实际得范围配置
    specialTemplate: any = {
        branchTemplate: {
            templateCode: '自动生成',
            boundType: '1',
            templateName: '',
        },
        riskEntryBranchs: [
            {
                entryBranch: {},
                entryBranchConfigs: [],
            },
        ],
    };
    boundTypes: any[] = [];
    stockBound: any[] = [];
    loading: boolean = false;

    async mounted() {
        this.boundTypes = DictionConstUtil.PojoToList(DictionConstUtil.BoundType).filter(item => ['1', '2', '3', '4'].includes(item.dictKey));
        this.getspecicl();
    }

    // 获取组件数据
    getBranchData(val: any) {
        this.specialTemplate.riskEntryBranchs[0].entryBranchConfigs = [];
        let data = {
            branchNo: 0,
            boundCode: '',
            boundGroupValid: true,
            boundValueNames: [],
            boundValues: [],
            children: [],
            compareType: '4',
            configId: 2,
            groupType: this.specialTemplate.branchTemplate.boundType,
            parentId: 0,
            separateValid: false,
            showBoundExtend: false,
            specialBranch: false,
            sqlSpliceType: '0',
            webBoundName: '',
            webBoundvalue: '',
            webCompareType: '',
            webSqlType: '并且',
        };
        for (let i = 0; i < val.length; i++) {
            // val[i].parentId = 2;
            val[i].groupType = '0'; //
        }
        this.specialTemplate.riskEntryBranchs[0].entryBranchConfigs.push(data);
        this.specialTemplate.riskEntryBranchs[0].entryBranchConfigs.push(...val);
        // if (this.specialTemplate.riskEntryBranchs[0].entryBranchConfigs.length > 0) {
        //     for (let i = 0; i < this.specialTemplate.riskEntryBranchs[0].entryBranchConfigs.length; i++) {
        //         if (this.specialTemplate.riskEntryBranchs[0].entryBranchConfigs[i].boundGroupValid === false) {
        //             this.specialTemplate.riskEntryBranchs[0].entryBranchConfigs[i].groupType = '0';
        //         }
        //     }
        // }
    }

    // 查看编辑数据
    getspecicl() {
        let url;
        if (this.itemData.type == 'check-bound') {
            url = '/entry/template/check/query/';
        } else {
            url = '/entry/template/bound/query/';
        }

        if (this.itemData.content) {
            this.loading = true;
            this.$axios.get(url + this.itemData.content).then((res: any) => {
                this.loading = false;
                this.specialTemplate.branchTemplate = res.branchTemplate;
                // this.specialTemplate.riskEntryBranchs = res.riskEntryBranchs;
                let configs = res.riskEntryBranchs[0].entryBranchConfigs;
                let entryBranchConfigs = [];
                for (let i = 0; i < configs.length; i++) {
                    if (configs[i].parentId !== 0) {
                        configs[i].groupType = this.specialTemplate.branchTemplate.boundType; //
                        entryBranchConfigs.push(configs[i]);
                    }
                }
                // for (let i = 0; i < entryBranchConfigs.length; i++) {
                //     entryBranchConfigs[i].parentId = 0;
                // }
                this.riskEntryBranchList = entryBranchConfigs;
                // console.log(this.riskEntryBranchList, ' this.riskEntryBranchList');
            });
        }
    }

    // 关闭
    openBranch() {
        this.$emit('close-custom', this.itemData);
    }

    // 提交
    submitSpecial() {
        console.log(this.specialTemplate);

        if (this.specialTemplate.branchTemplate.templateName == '') {
            this.yh_message_error('模板名称不能为空');
            return;
        }

        let url = '';
        this.loading = true;
        if (this.itemData.type === 'add-bound' || this.itemData.type === 'copy-bound') {
            url = '/entry/template/bound/insert';
        } else if (this.itemData.type === 'edit-bound') {
            url = '/entry/template/bound/update';
        }
        this.loading = true;
        this.$axios
            .post(url, this.specialTemplate)
            .then(res => {
                this.loading = false;
                this.yh_message_success('操作成功');
                this.openBranch();
                DictionConstUtil.FNBranchBoundTemplateRefresh();
            })
            .catch(() => {
                this.loading = false;
            });
    }

    // 范围类型改变
    changeBoundType() {
        this.riskEntryBranchList = [];
        this.specialTemplate.riskEntryBranchs[0].entryBranchConfigs = [];
    }
}
</script>
<style lang="scss" scoped>
.sp-detail {
    color: #222;
    padding: 10px;
    overflow: scroll;

    .sp-top {
        width: 100%;
        border: 1px solid #eee;
        padding: 10px 0;
        min-width: 1000px;
        height: 100px;
        margin-bottom: 10px;

        & /deep/ .el-form-item--mini .el-form-item__label {
            color: #222;
        }
    }

    .sp-itme {
        min-width: 1000px;
        overflow-x: hidden;
        margin-top: 20px;
    }

    .sp-bout {
        border: 1px solid #eee;
        padding: 10px;
        height: 55px;
        min-width: 700px;

        .sp-but-div {
            float: right;
        }
    }
}
</style>
